﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WinFormCenter.page.互评 {
    public class MathQuizForm : Form {
        // 控件声明
        private Label label1;
        private TextBox txtNum1;
        private Label lblOperator;
        private TextBox txtNum2;
        private Label labelEquals;
        private TextBox txtAnswer;
        private Label labelAnswer;
        private Button btnSubmit;
        private Label lblResult;
        private Label lblScore;
        private System.Windows.Forms.Timer timer;
        private Button btnStart;
        private Button btnStop;
        private Label lblTimer;

        // 变量声明
        private Random random;
        private int score;
        private int correctAnswer;
        private int timeLeft;
        private int totalQuestions;
        private System.ComponentModel.IContainer components;
        private int correctQuestions;

        public MathQuizForm() {
            InitializeComponent();
            InitializeApp();
        }

        private void InitializeComponent() {
            components = new System.ComponentModel.Container();
            label1 = new Label();
            txtNum1 = new TextBox();
            lblOperator = new Label();
            txtNum2 = new TextBox();
            labelEquals = new Label();
            txtAnswer = new TextBox();
            labelAnswer = new Label();
            btnSubmit = new Button();
            lblResult = new Label();
            lblScore = new Label();
            timer = new System.Windows.Forms.Timer(components);
            btnStart = new Button();
            btnStop = new Button();
            lblTimer = new Label();
            SuspendLayout();
            // 
            // label1
            // 
            label1.Location = new Point(20, 20);
            label1.Name = "label1";
            label1.Size = new Size(67, 74);
            label1.TabIndex = 0;
            label1.Text = "题目：";
            label1.TextAlign = ContentAlignment.MiddleLeft;
            // 
            // txtNum1
            // 
            txtNum1.Location = new Point(80, 20);
            txtNum1.Name = "txtNum1";
            txtNum1.ReadOnly = true;
            txtNum1.Size = new Size(54, 38);
            txtNum1.TabIndex = 1;
            txtNum1.TextAlign = HorizontalAlignment.Center;
            // 
            // lblOperator
            // 
            lblOperator.Location = new Point(140, 28);
            lblOperator.Name = "lblOperator";
            lblOperator.Size = new Size(20, 20);
            lblOperator.TabIndex = 2;
            lblOperator.Text = "+";
            lblOperator.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // txtNum2
            // 
            txtNum2.Location = new Point(180, 20);
            txtNum2.Name = "txtNum2";
            txtNum2.ReadOnly = true;
            txtNum2.Size = new Size(50, 38);
            txtNum2.TabIndex = 3;
            txtNum2.TextAlign = HorizontalAlignment.Center;
            // 
            // labelEquals
            // 
            labelEquals.Location = new Point(328, 28);
            labelEquals.Name = "labelEquals";
            labelEquals.Size = new Size(20, 20);
            labelEquals.TabIndex = 4;
            labelEquals.Text = "=";
            labelEquals.TextAlign = ContentAlignment.MiddleCenter;
            // 
            // txtAnswer
            // 
            txtAnswer.Location = new Point(446, 20);
            txtAnswer.Name = "txtAnswer";
            txtAnswer.Size = new Size(60, 38);
            txtAnswer.TabIndex = 5;
            txtAnswer.TextAlign = HorizontalAlignment.Center;
            // 
            // labelAnswer
            // 
            labelAnswer.Location = new Point(20, 284);
            labelAnswer.Name = "labelAnswer";
            labelAnswer.Size = new Size(231, 68);
            labelAnswer.TabIndex = 6;
            labelAnswer.Text = "你的答案：";
            // 
            // btnSubmit
            // 
            btnSubmit.BackColor = Color.LightBlue;
            btnSubmit.Location = new Point(653, 28);
            btnSubmit.Name = "btnSubmit";
            btnSubmit.Size = new Size(76, 58);
            btnSubmit.TabIndex = 7;
            btnSubmit.Text = "提交";
            btnSubmit.UseVisualStyleBackColor = false;
            // 
            // lblResult
            // 
            lblResult.Font = new Font("Microsoft Sans Serif", 9F, FontStyle.Bold);
            lblResult.Location = new Point(100, 60);
            lblResult.Name = "lblResult";
            lblResult.Size = new Size(406, 193);
            lblResult.TabIndex = 8;
            // 
            // lblScore
            // 
            lblScore.Location = new Point(20, 399);
            lblScore.Name = "lblScore";
            lblScore.Size = new Size(140, 38);
            lblScore.TabIndex = 9;
            lblScore.Text = "得分：0分";
            // 
            // btnStart
            // 
            btnStart.BackColor = Color.LightGreen;
            btnStart.Location = new Point(630, 200);
            btnStart.Name = "btnStart";
            btnStart.Size = new Size(64, 68);
            btnStart.TabIndex = 10;
            btnStart.Text = "开始";
            btnStart.UseVisualStyleBackColor = false;
            // 
            // btnStop
            // 
            btnStop.BackColor = Color.LightCoral;
            btnStop.Enabled = false;
            btnStop.Location = new Point(838, 200);
            btnStop.Name = "btnStop";
            btnStop.Size = new Size(66, 68);
            btnStop.TabIndex = 11;
            btnStop.Text = "停止";
            btnStop.UseVisualStyleBackColor = false;
            // 
            // lblTimer
            // 
            lblTimer.Location = new Point(20, 486);
            lblTimer.Name = "lblTimer";
            lblTimer.Size = new Size(190, 62);
            lblTimer.TabIndex = 12;
            lblTimer.Text = "下一题倒计时：5秒";
            // 
            // MathQuizForm
            // 
            ClientSize = new Size(974, 929);
            Controls.Add(label1);
            Controls.Add(txtNum1);
            Controls.Add(lblOperator);
            Controls.Add(txtNum2);
            Controls.Add(labelEquals);
            Controls.Add(txtAnswer);
            Controls.Add(labelAnswer);
            Controls.Add(btnSubmit);
            Controls.Add(lblResult);
            Controls.Add(lblScore);
            Controls.Add(btnStart);
            Controls.Add(btnStop);
            Controls.Add(lblTimer);
            FormBorderStyle = FormBorderStyle.FixedSingle;
            MaximizeBox = false;
            Name = "MathQuizForm";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "自动出题判分系统";
            ResumeLayout(false);
            PerformLayout();
        }

        private void InitializeApp() {
            random = new Random();
            score = 0;
            totalQuestions = 0;
            correctQuestions = 0;
            timeLeft = 5;

            // 设置定时器
            timer.Interval = 1000; // 1秒
            timer.Tick += Timer_Tick;

            // 绑定事件
            btnSubmit.Click += BtnSubmit_Click;
            btnStart.Click += BtnStart_Click;
            btnStop.Click += BtnStop_Click;
            txtAnswer.KeyPress += TxtAnswer_KeyPress;

            // 生成第一题
            GenerateQuestion();
        }

        private void GenerateQuestion() {
            // 生成两个随机数（1-50，避免数字太大）
            int num1 = random.Next(1, 51);
            int num2 = random.Next(1, 51);

            // 随机选择加法或减法
            string op = random.Next(0, 2) == 0 ? "+" : "-";

            // 计算正确答案
            if (op == "+") {
                correctAnswer = num1 + num2;
            } else {
                // 确保减法结果不为负数
                if (num1 < num2) {
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                correctAnswer = num1 - num2;
            }

            // 更新界面
            txtNum1.Text = num1.ToString();
            txtNum2.Text = num2.ToString();
            lblOperator.Text = op;
            txtAnswer.Text = "";
            txtAnswer.BackColor = Color.White;
            lblResult.Text = "";

            // 重置倒计时
            timeLeft = 5;
            UpdateTimerDisplay();

            // 焦点设置到答案框
            txtAnswer.Focus();
        }

        private void UpdateTimerDisplay() {
            lblTimer.Text = $"下一题倒计时：{timeLeft}秒";
            lblTimer.ForeColor = timeLeft <= 3 ? Color.Red : Color.Black;
        }

        private void CheckAnswer() {
            if (string.IsNullOrEmpty(txtAnswer.Text.Trim())) {
                MessageBox.Show("请输入答案！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (int.TryParse(txtAnswer.Text.Trim(), out int userAnswer)) {
                totalQuestions++;

                // 使用if语句判断答案
                if (userAnswer == correctAnswer) {
                    // 答案正确
                    score += 10;
                    correctQuestions++;
                    lblResult.Text = "正确！✓";
                    lblResult.ForeColor = Color.Green;
                    txtAnswer.BackColor = Color.LightGreen;

                    // 使用switch语句显示不同的鼓励消息
                    switch (correctQuestions) {
                        case 1:
                            ShowMessage("好样的！答对第一题！");
                            break;
                        case 5:
                            ShowMessage("太棒了！你已经答对5题了！");
                            break;
                        case 10:
                            ShowMessage("完美！你是数学小天才！");
                            break;
                    }
                } else {
                    // 答案错误
                    lblResult.Text = $"错误！正确答案是：{correctAnswer}";
                    lblResult.ForeColor = Color.Red;
                    txtAnswer.BackColor = Color.LightPink;
                }

                // 更新得分显示
                double accuracy = totalQuestions > 0 ? (double)correctQuestions / totalQuestions * 100 : 0;
                lblScore.Text = $"得分：{score}分 (正确率：{accuracy:F1}%)";

                // 禁用提交按钮，等待自动出题
                btnSubmit.Enabled = false;
            } else {
                MessageBox.Show("请输入有效的数字！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAnswer.Focus();
                txtAnswer.SelectAll();
            }
        }

        private void ShowMessage(string message) {
            MessageBox.Show(message, "鼓励", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // 事件处理方法
        private void Timer_Tick(object sender, EventArgs e) {
            timeLeft--;
            UpdateTimerDisplay();

            if (timeLeft <= 0) {
                GenerateQuestion();
                btnSubmit.Enabled = true;
            }
        }

        private void BtnSubmit_Click(object sender, EventArgs e) {
            CheckAnswer();
        }

        private void BtnStart_Click(object sender, EventArgs e) {
            timer.Start();
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnSubmit.Enabled = true;
            txtAnswer.Focus();
        }

        private void BtnStop_Click(object sender, EventArgs e) {
            timer.Stop();
            btnStart.Enabled = true;
            btnStop.Enabled = false;
            btnSubmit.Enabled = false;

            // 显示最终成绩
            double accuracy = totalQuestions > 0 ? (double)correctQuestions / totalQuestions * 100 : 0;
            string resultMessage = $"游戏结束！\n\n最终统计：\n" +
                                 $"总题数：{totalQuestions}题\n" +
                                 $"答对题数：{correctQuestions}题\n" +
                                 $"最终得分：{score}分\n" +
                                 $"正确率：{accuracy:F1}%";

            MessageBox.Show(resultMessage, "成绩统计", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void TxtAnswer_KeyPress(object sender, KeyPressEventArgs e) {
            // 只允许输入数字、退格键
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) {
                e.Handled = true;
            }

            // 允许按回车键提交答案
            if (e.KeyChar == (char)Keys.Enter && btnSubmit.Enabled) {
                CheckAnswer();
                e.Handled = true;
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e) {
            timer.Stop();
            base.OnFormClosing(e);
        }
    }

    //internal static class Program {
    //    [STAThread]
    //    static void Main() {
    //        Application.EnableVisualStyles();
    //        Application.SetCompatibleTextRenderingDefault(false);

    //        try {
    //            Application.Run(new MathQuizForm());
    //        } catch (Exception ex) {
    //            MessageBox.Show($"程序启动时发生错误：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    //        }
    //    }
    //}
}
